package com.tsinghuabigdata.edu.ddmath.module.ddwork.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

import com.tsinghuabigdata.edu.ddmath.R;
import com.tsinghuabigdata.edu.ddmath.module.ddwork.bean.DDWorkDetail;
import com.tsinghuabigdata.edu.ddmath.module.ddwork.bean.LocalPageInfo;
import com.tsinghuabigdata.edu.ddmath.module.ddwork.view.PageItemView;

/**
 * 待上传作业列表 适配器
 */
public class DDWorkPageAdapter extends ArrayAdapter<LocalPageInfo> {

    private Context mContext;
//    private ListView parentListView;
//    UploadManager uploadManager;
//    private Drawable delDrawable;
//
//    private boolean hasUploading = false;

    private int submitStatus = 0;       //作业提交状态

    public DDWorkPageAdapter(Context context, int status) {
        super(context, 0);
        mContext = context;
        submitStatus = status;
        //uploadManager = UploadManager.getUploadManager( mContext );
        //delDrawable = context.getResources().getDrawable( R.drawable.ic_delete );
    }

    public void selectItem( int position ){
        int count = getCount();
        for( int i=0; i<count; i++ ){
            LocalPageInfo item = getItem( i );
            if( position == i ) item.setSelected( true );
            else item.setSelected( false );
        }
        notifyDataSetChanged();
    }

    public int getSelectIndex(){
        int count = getCount();
        for( int i=0; i<count; i++ ){
            LocalPageInfo item = getItem( i );
            if( getItem(i).isSelected() ) return i;
        }
        return -1;
    }

//    public void setHasUploading(boolean has){
//        hasUploading = has;
//    }
//    public void setParentListView(ListView listView ){
//        parentListView = listView;
//    }



    class ViewHolder{

        //private LinearLayout mainLayout;
        private int position;

        private PageItemView pageItemView;

        private ViewHolder(View convertView) {
            //mainLayout       = (LinearLayout) convertView.findViewById( R.id.mylearn_waitwork_mainlayout );
            pageItemView     = (PageItemView)convertView.findViewById( R.id.item_ddwork_pageview );
        }

        public void bindView( LocalPageInfo pageInfo, ViewHolder viewHolder, int position ){
            this.position = position;
            if( submitStatus == DDWorkDetail.WORK_SUBMITED){        //作业已提交
                pageItemView.setData( pageInfo.getPageNum(), pageInfo.getSubmitStatus()==1?PageItemView.ST_COMMITED:PageItemView.ST_UNCOMMIT, pageInfo.isSelected() );
            }else{
                if( pageInfo.getUploadStatus() == LocalPageInfo.ST_NONE ){  //  非上传状态
                    boolean hasimage = !TextUtils.isEmpty( pageInfo.getLocalpath() );
                    pageItemView.setData( pageInfo.getPageNum(), hasimage? PageItemView.ST_CAMERA : PageItemView.ST_UNCAMERA, pageInfo.isSelected() );
                }else{  //上传状态
                    pageItemView.setData( pageInfo.getPageNum(), pageInfo.getUploadStatus()+PageItemView.ST_CAMERA, pageInfo.isSelected() );
                }
            }
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate( R.layout.item_ddwork_page, parent, false);
            convertView.setTag(new ViewHolder(convertView));
        }
        ViewHolder viewHolder = ((ViewHolder) convertView.getTag());
        viewHolder.bindView( getItem(position), viewHolder, position);
        return convertView;
    }


//    private void setImageAdapter( WaitWorkBean bean, ViewHolder viewHolder) {
//        if (viewHolder.gridView.getAdapter() == null) {
//            WaitImageAdapter adapter = new WaitImageAdapter( mContext, this );
//            adapter.setData(bean.getImageList(),viewHolder, bean);
//            viewHolder.gridView.setAdapter(adapter);
//        } else {
//            WaitImageAdapter adapter = (WaitImageAdapter) viewHolder.gridView.getAdapter();
//            adapter.setData(bean.getImageList(),viewHolder, bean);
//            adapter.notifyDataSetChanged();
//        }
//    }


}